﻿using System;
using Common;
using UnityEngine;

namespace UI
{
    public class UILayer
    {

        public const int LayerIndex_UI = 0;
        public const int LayerIndex_UI3D = 1;
        public const int LayerIndex_UI_1 = 2;
        public const int LayerIndex_UI3D_1 = 3;
        public const int LayerIndex_UI_2 = 4;
        public const int LayerIndex_UI3D_2 = 5;
        public const int LayerIndex_UI_3 = 6;
        public const int LayerIndex_UI3D_3 = 7;
        
        
        private const int StartCameraDepth = 100;

        public Transform root { get; private set; }
        public Transform canvasRoot { get; private set; }
        private readonly Camera m_CameraUI;
        private readonly Camera m_Camera3D;
        private readonly int m_UiLayerIndex;

        private int m_ActivePanelCounter = 0;
        private bool m_isUICameraLock = false;
        private bool m_is3DCameraLock = false;
        
        public UILayer(Transform root, int uiLayerIndex)
        {
            this.root = root;
            canvasRoot = root.Find("Canvas");
            m_CameraUI = root.Find("Camera/Camera_UI").GetComponent<Camera>();
            m_Camera3D = root.Find("Camera/Camera_3D").GetComponent<Camera>();
            
            m_UiLayerIndex = uiLayerIndex;
            m_CameraUI.depth = StartCameraDepth + uiLayerIndex * 2;
            m_Camera3D.depth = StartCameraDepth + uiLayerIndex * 2 + 1;
            
            SwitchCullingMaskEnable( true );
            SetCameraAble(false);
        }
        
        
        public void SwitchCullingMaskEnable( bool enable)
        {
            if (enable)
            {
                m_CameraUI.cullingMask = LayerUtility.UILayers[m_UiLayerIndex];
                m_Camera3D.cullingMask = LayerUtility.UILayers[m_UiLayerIndex + 1];
            }
            else
            {
                m_CameraUI.cullingMask = 0;
                m_Camera3D.cullingMask = 0;
            }
        }
        
        public void SetCameraAble(bool able)
        {
            SetUICameraAble(able);
            Set3DCameraAble(able);
        }
        
        public void SetUICameraAble(bool active)
        {
            m_ActivePanelCounter = active ? ++m_ActivePanelCounter : --m_ActivePanelCounter;
            m_ActivePanelCounter = Math.Max(0, m_ActivePanelCounter);
            if (m_isUICameraLock)
            {
                return;
            }
            m_CameraUI.enabled = m_ActivePanelCounter > 0;
        }
        
        public void Set3DCameraAble(bool active)
        {

            if (m_is3DCameraLock)
            {
                return;
            }

            m_Camera3D.enabled = active;
        }
        
        public void LockUICameraEnable(bool lockState)
        {
            m_isUICameraLock = true;
            m_CameraUI.enabled = lockState;
        }
        
        public void UnlockUICameraEnable()
        {
            m_isUICameraLock = false;
            m_CameraUI.enabled = m_ActivePanelCounter > 0;
        }
        
        public void Lock3DCameraEnable(bool lockState)
        {
            m_is3DCameraLock = true;
            m_Camera3D.enabled = lockState;
        }
        
        public void Unlock3DCameraEnable()
        {
            m_is3DCameraLock = false;
            // m_Camera3D.enabled = m_ActivePanelCounter > 0;
        }
        
        public void SetUICameraTop(bool top)
        {
            m_CameraUI.depth = top ? StartCameraDepth + m_UiLayerIndex * 2 + 1 : StartCameraDepth + m_UiLayerIndex * 2;
            m_Camera3D.depth = top ? StartCameraDepth + m_UiLayerIndex * 2 : StartCameraDepth + m_UiLayerIndex * 2 + 1;
        }

        public void AddPanel(GameObject go)
        {
            go.transform.SetParent(canvasRoot);
        }

    }
}